from selenium.webdriver import Edge
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
import csv

# f = open('招聘数据.csv', mode='a', encoding='utf-8', newline='')
# csv_writer = csv.DictWriter(f,fieldnames=[
#     '标题',
#     '地区',
#     '薪资',
#     '经验',
#     '学历',
#     '公司',
#     '公司类型',
#     '福利',
# ])
# csv_writer.writeheader()

import time

driver = Edge(executable_path='../203 手机壁纸/msedgedriver.exe')

driver.get('https://www.zhipin.com/')

driver.implicitly_wait(10)  # 等网页数据加载完就可以了
driver.find_element(By.XPATH, '//*[@id="wrap"]/div[3]/div/div[1]/div[1]/form/div[2]/p/input').send_keys('python')   # 输入
driver.find_element(By.XPATH, '//*[@id="wrap"]/div[3]/div/div[1]/div[1]/form/div[2]/p/input').send_keys(Keys.ENTER)   # 键盘输入
# driver.find_element(By.XPATH, '//*[@id="wrap"]/div[3]/div/div[1]/div[1]/form/button').click()   # 点击
driver.implicitly_wait(10)


# lis = driver.find_elements(By.XPATH, '//*[@id="wrap"]/div[2]/div[2]/div/div[1]/div[2]/ul/li')
lis = driver.find_elements(By.CSS_SELECTOR, '#wrap > div.page-job-wrapper > div.page-job-inner > div > div.job-list-wrapper > div.search-job-result > ul > li')
# print(lis)
for li in lis:
    # 标题
    title = li.find_element(By.XPATH, './div/a/div[1]/span[1]').text
    # 地区
    area = li.find_element(By.XPATH, './div/a/div[1]/span[2]/span').text
    # 薪资
    salary = li.find_element(By.XPATH, './div/a/div[2]/span').text
    # 经验
    exp = li.find_element(By.XPATH, './div/a/div[2]/ul/li[1]').text
    # 学历
    ab = li.find_element(By.XPATH, './div/a/div[2]/ul/li[2]').text
    # 公司
    company = li.find_element(By.XPATH, './div/div/div[2]/h3/a').text
    # 公司类型
    company_type = li.find_element(By.XPATH, './div/div/div[2]/ul/li[1]').text
    # 福利
    desc = li.find_element(By.CSS_SELECTOR, './info-desc').text
    # herf
    # her = li.find_element(By.CSS_SELECTOR, '.job-card-left').get_attribute('herf')
    # dic = {
    #     '标题': title,
    #     '地区': area,
    #     '薪资': salary,
    #     '经验': exp,
    #     '学历': ab,
    #     '公司': company,
    #     '公司类型': company_type,
    #     '福利': desc
    # }
    # csv_writer.writerow(dic)
    print(title, area, salary, exp, ab, company, company_type, desc)
driver.quit()   # 退出浏览器











